package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.BaseMenu;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.service.BaseMenuService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.DateUtil;
import ysq.xyg.util.ProProperties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@Controller
public class CommonController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(CommonController.class);
    @Resource
    private BaseMenuService baseMenuService;
    /**
     *
     * */
    @RequestMapping(value="/{path}.txt")
    public void checkTxt(HttpServletRequest request,HttpServletResponse response,@PathVariable("path") String path) throws IOException {
        String realPath = request.getSession().getServletContext().getRealPath(ProProperties.getInstance().getPictureURL());
        File file = new File(realPath+"/"+path+".txt");
        OutputStream os = response.getOutputStream();
        FileInputStream fips = new FileInputStream(file);
        ByteArrayOutputStream bops = new ByteArrayOutputStream();
        int data = -1;
        while ((data = fips.read()) != -1) {
            bops.write(data);
        }
        byte[] btImg = bops.toByteArray();
        os.write(btImg);
        os.flush();
    }
    /**
     * 登录页面（默认是首页）
     * */
    @RequestMapping(value="/managerLogin")
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
        BaseUserInfo baseUserInfo=new BaseUserInfo();
        baseUserInfo.setUserid(BaseController.CookiesAcceptObject(request, BaseConstant.COOKIE_LOGINNAME));
        baseUserInfo.setUserpassword(BaseController.CookiesAcceptObject(request, BaseConstant.COOKIE_PASSWORD));
        if(baseUserInfo.getUserid()!=null && !"".equals(baseUserInfo.getUserid()) && baseUserInfo.getUserpassword()!=null && !"".equals(baseUserInfo.getUserpassword())){
            baseUserInfo.setAutoLogin(2);
        }else{
            baseUserInfo.setAutoLogin(1);
        }
        log.info("baseUserInfo-"+baseUserInfo.toString());
        return new ModelAndView("/login").addObject(baseUserInfo);
    }
    /**
     * 登录进入默认页面
     * */
    @RequestMapping(value="/main")
    public ModelAndView main(HttpServletRequest request,HttpServletResponse response){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        Date newDate=new Date();
        List<String> dataList=new ArrayList<String>();
        List<Integer> memberList=new ArrayList<Integer>();
        List<Double> recordList=new ArrayList<Double>();
        /*Integer currentAllMember=memberService.selectMemberCount(new Member());
        EntryFormOne entryFormOne=new EntryFormOne();
        entryFormOne.setStatus("2");//已交费
        Double allOrderCount=entryFormOneService.selectAllCount(entryFormOne);
        entryFormOne.setStartDate(DateUtil.DateToStr(newDate, DateUtil.DATE_PATTERN)+" 00:00:00");
        Double currentOrderCount=entryFormOneService.selectAllCount(entryFormOne);

        //获取最近7天
        for(int i=0;i<7;i++){
            dataList.add(DateUtil.DateToStr(DateUtil.getDateOfDate(newDate,i), DateUtil.DATE_PATTERN));
            Member memberEntity=new Member();
            memberEntity.setStartDate(dataList.get(i)+" 00:00:00");
            memberEntity.setEndDate(dataList.get(i)+" 23:59:59");
            int  mSize= memberService.selectMemberCount(memberEntity);

            EntryFormOne entryFormOnes=new EntryFormOne();
            entryFormOnes.setStatus("2");//已交费
            entryFormOnes.setStartDate(dataList.get(i)+" 00:00:00");
            entryFormOnes.setEndDate(dataList.get(i)+" 23:59:59");
            Double pSize=entryFormOneService.selectAllCount(entryFormOnes);
            memberList.add(mSize);
            recordList.add(pSize);
        }
*/
        return new ModelAndView("/baseMain").addObject("baseUserInfo",baseUserInfo)
                /*.addObject("currentAllMember",currentAllMember)
                .addObject("allOrderCount",allOrderCount)
                .addObject("currentOrderCount",currentOrderCount)*/
                .addObject("dataList", JSONArray.toJSONString(dataList))
                .addObject("memberList",JSONArray.toJSONString(memberList))
                .addObject("recordList",JSONArray.toJSONString(recordList))
                ;
    }

    /**
     * 个人中心页面
     * */
    @RequestMapping(value="/index")
    public ModelAndView Index(HttpServletRequest request,HttpServletResponse response){
        BaseUserInfo baseUserInfo=acceptCurrentUser(request);
        //菜单查询
        BaseMenu baseMenu=new BaseMenu();
        baseMenu.setRemovetag(BaseConstant.REMOVE_N);
        baseMenu.setMenugrade(BaseConstant.MENU_ONE);
        baseMenu.setDepartId(baseUserInfo.getDepartid());
        List<BaseMenu> baseMenuList=baseMenuService.selectEntityByLeft(baseMenu);
        ModelAndView modelAndView=new ModelAndView("/index").addObject("baseMenuList",baseMenuList).addObject("baseMenu",baseMenuList.get(0)).addObject("baseUserInfo",baseUserInfo);
        if(baseMenuList!=null&&baseMenuList.size()>0){
            BaseMenu baseMenus=new BaseMenu();
            baseMenus.setRemovetag(BaseConstant.REMOVE_N);
            baseMenus.setMenugrade(BaseConstant.MENU_TWO);
            baseMenus.setParentmenuid(baseMenuList.get(0).getMenuid());
            List<BaseMenu> baseMenuLists=baseMenuService.selectEntityByLeft(baseMenus);
            modelAndView.addObject("url",baseMenuLists.get(0).getMenuurl());
        }

        return modelAndView;
    }

}
